// shop/pages/deliveraddress/index.js
import api from '../../../utils/api'

Page({
  data: {
    defaultSize: 'mini',
    disabled: false,
    plain: true,
    loading: false,
    addressList: [
        {
            address: '三栋302室',
            receiver: '张三',
            mobile: '13333333333',
            addressregion: '上海ABC区420号',
        }
    ],
    addAddress: '',
    order: -1
  },

  onLoad: function (option) {
    if (option.order) {
      this.setData({
        order: option.order
      });
    }
  },
  optionCollect(e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/shop/pages/adddeliveraddress/index?id=${id}`,
    })
  },
/**
     * 删除 
     */
    onDel(e) {
        const _this = this
        const value = e.currentTarget.dataset.id
        wx.showModal({
          title: '',
          content: '确认要删除该地址吗？',
          confirmColor: "#eb2444",
          success(res) {
            if (res.confirm) {
              wx.showLoading({
                  mask: true
              });
              api.get(`/api/hxhealth/deliveraddress/remove/${value}`, {
                  data: {value: value}
              }).then(res => {
                  wx.showToast({
                      title: '删除成功',
                      complete: () => {
                        _this.onShow()
                      }
                  })
                  
              }).catch(e => {
                  wx.showToast({
                      icon: 'none',
                      title: '删除失败：' + e,
                  })
              })
            }
          }
        })
    },
  //新增收货地址
  onAddAddr: function (e) {
    wx.navigateTo({
      url: '/shop/pages/adddeliveraddress/index',
    })
  },

  //设置为默认地址
  onDefaultAddr: function (e) {
    var addrId = e.currentTarget.dataset.addrid;
        api.get(`/api/hxhealth/deliveraddress/setdefault/${addrId}`).then(res => {
            wx.showToast({
                icon: 'none',
                title: '设置完成'
            })
        }).catch(e => {
            wx.showToast({
                icon: 'none',
                title: '设置失败：' + e,
            })
        })
  },

  //加载地址列表
  onShow: function () {
    const _this = this
    wx.showLoading();
    try {
        api.post('/api/hxhealth/deliveraddress/list').then((res) => {
            console.log('address====', res);
            _this.setData({
                addressList: res || []
            });
            wx.hideLoading();
        });
    } catch (e) {
        wx.hideLoading();
        console.error(e);
    }
  },

// 修改地址 
  toEditAddress: function (e) {
    var addrId = e.currentTarget.dataset.addrid;
    wx.navigateTo({
      url: '/pages/editAddress/editAddress?addrId=' + addrId,
    })
  },

  /**
   * 选择地址 跳转回提交订单页
   */
  selAddrToOrder: function (e) {
    if (this.data.order == 0) {
      var pages = getCurrentPages();//当前页面
      var prevPage = pages[pages.length - 2];//上一页面
      prevPage.setData({//直接给上移页面赋值
        item: e.currentTarget.dataset.item,
        selAddress: 'yes'
      });
      wx.navigateBack({//返回
        delta: 1
      })
    }
  }
})